<template>
  <div style="background-color: #f0f2f5">
    <el-collapse
      v-model="activeNames"
      style="margin: 30px"
      @change="handleChange"
    >
      <el-collapse-item
        title="一致性 Consistency"
        name="1"
        style="padding: 10px"
      >
        <el-form
          :inline="true"
          :model="formInline"
          class="demo-form-inline"
          style="padding: 10px"
        >
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="商品名称">
                <el-input v-model="formInline.name" placeholder="商品名称" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="商品货号">
                <el-input
                  v-model="formInline.productSn"
                  placeholder="商品货号"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="品牌id">
                <el-select v-model="formInline.brandId" placeholder="品牌id">
                  <el-option
                    v-for="(item, index) in parduct"
                    :key="index"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="上架状态">
                <el-select
                  v-model="formInline.publishStatus"
                  placeholder="上架状态"
                >
                  <el-option label="上架" :value="0" />
                  <el-option label="未上架" :value="1" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="审核状态">
                <el-select
                  v-model="formInline.verifyStatus"
                  placeholder="审核状态"
                >
                  <el-option label="审核" :value="0" />
                  <el-option label="未审核" :value="1" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6" :offset="12">
              <el-button type="text" @click="reset">重置</el-button>
              <el-button type="primary" @click="seach">查询</el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-collapse-item>
    </el-collapse>
    <el-card class="box-card" style="margin: 30px">
      <div slot="header" class="clearfix">
        <el-button
          style="padding: 10px"
          type="text"
          @click="addcaerted"
        >新增</el-button>
        <el-button style="padding: 10px" type="text">操作按钮</el-button>
      </div>
      <el-table :data="list" border style="width: 100%">
        <el-table-column
          fixed
          type="index"
          align="center"
          label="序号"
          width="70"
        />
        <el-table-column prop="pic" label="商品图片" align="center" width="150">
          <template slot-scope="scope">
            <img
              :src="scope.row.pic"
              alt=""
              style="width: 60px; height: 60px"
            >
          </template>
        </el-table-column>
        <el-table-column prop="name" label="商品名称" width="150">
          <template slot-scope="scope">
            <p>{{ scope.row.name }}</p>
            <p>品牌:{{ scope.row.brandName }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="姓名" align="center" width="120">
          <template slot-scope="scope">
            <p>原价:{{ scope.row.price }}</p>
            <p>现价:{{ scope.row.originalPrice }}</p>
          </template>
        </el-table-column>
        <el-table-column
          prop="productCategoryName"
          label="商品类别"
          align="center"
          width="120"
        />
        <el-table-column
          prop="newStatus "
          label="标签"
          align="center"
          width="120"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.newStatus"
              active-color="#13ce66"
              inactive-color="#ff4949"
              inactive-text="最新"
              :active-value="1"
              :inactive-value="0"
              @change="newest(scope.row)"
            />
            <el-switch
              v-model="scope.row.recommendStatus"
              active-color="#13ce66"
              inactive-color="#ff4949"
              inactive-text="推荐"
              :active-value="1"
              :inactive-value="0"
              @change="recommend(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="newStatus "
          label="标签1"
          align="center"
          width="120"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.publishStatus"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              inactive-text="发布"
              :inactive-value="0"
              @change="issue(scope.row)"
            />
            <el-switch
              v-model="scope.row.verifyStatus"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              inactive-text="审核"
              :inactive-value="0"
              @change="audit(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="productCategoryName"
          label="sku"
          align="center"
          width="120"
        >
          <template slot-scope="scope">
            <p style="color: blue" @click="addsku(scope.row)">编辑sku</p>
          </template>
        </el-table-column>
        <el-table-column
          prop="weight"
          label="重量"
          align="center"
          width="120"
        />
        <el-table-column prop="sort" label="排序" align="center" width="120" />
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handleClick(scope.row)"
            >编辑</el-button>
            <el-button
              type="text"
              size="small"
              style="color: red"
              @click="delPRT(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="doot.start"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="doot.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <el-dialog title="SKU" :visible.sync="dialogTableVisible" width="70%">
      <el-button type="primary" @click="addskulist">新增</el-button>
      <el-table
        v-if="ruleForm.pmsSkuStockList.length > 0"
        :data="ruleForm.pmsSkuStockList"
        border
        height="250"
        style="width: 100%"
      >
        <el-table-column fixed type="index" label="序列" width="50" />
        <el-table-column prop="pic" label="图片" width="120">
          <template slot-scope="scope">
            <div class="grid-content bg-purple" style="height: 150px">
              添加图片
              <el-upload
                class="upload-demo"
                action="/lejuAdmin/material/uploadFileOssSave"
                :headers="headers"
                :show-file-list="false"
                :on-success="handleAvatarSuccessSKU"
              >
                <img
                  v-if="scope.row.pic"
                  :src="scope.row.pic"
                  class="avatar"
                  style="width: 100px; height: 100px"
                >
                <i
                  v-else
                  class="el-icon-plus avatar-uploader-icon"
                  style="width: 60px; height: 60px"
                  @click="seletkrow(scope.row)"
                />
              </el-upload>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="color" label="颜色" width="120">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.color"
              placeholder="请输入颜色"
              clearable
            />
          </template>
        </el-table-column>

        <el-table-column prop="size" label="大小" width="120">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.size"
              placeholder="请输入大小"
              clearable
            />
          </template>
        </el-table-column>
        <el-table-column prop="stock" label="库存" width="300">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.stock"
              placeholder="请输入库存"
              clearable
            />
          </template>
        </el-table-column>
        <el-table-column prop="lowStock" label="预警库存" width="120">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.lowStock"
              placeholder="预警库存"
              clearable
            />
          </template>
        </el-table-column>
        <el-table-column prop="lockStock" label="锁定库存" width="120">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.lockStock"
              placeholder="锁定库存"
              clearable
            />
          </template>
        </el-table-column>
        <el-table-column prop="skuCode" label="sku编码" width="120">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.skuCode"
              placeholder="sku编码"
              clearable
            />
          </template>
        </el-table-column>
        <el-table-column prop="price" label="价格" width="120">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.price"
              placeholder="请输入价格"
              clearable
            />
          </template>
        </el-table-column>
        <el-table-column prop="sale" label="销量" width="120">
          <template slot-scope="scope">
            <el-input v-model="scope.row.sale" placeholder="销量" clearable />
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              style="color: red"
              @click="delrow(scope.row)"
            >删除</el-button>
            <el-button
              type="text"
              size="small"
              @click="updata(scope.row)"
            >保存</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script >
import { getToken } from '@/utils/myAuth'
import {
  findAllBrand,
  productsByPage,
  switchNewStatus,
  switchRecommandStatus,
  switchPublishStatus,
  switchVerifyStatus,
  delproduct,
  productSkusDetail,
  updateProductAndSkus
} from '@/api/product/index'
export default {
  components: {},
  data() {
    return {
      ruleForm: {
        pmsSkuStockList: []
      }, // 表格
      dialogTableVisible: false, // 打开弹出框
      activeNames: ['0'], // 表头折叠
      formInline: {
        name: '', // 商品名称
        productSn: '', // 商品货号
        brandId: '', // 品牌id
        publishStatus: '', // 上架状态
        verifyStatus: '' // 审核状态
      }, // 查询表单
      doot: {
        start: 1,
        limit: 10
      },
      parduct: [], // 品牌列表
      list: [], // 商品列表
      total: 0, // 商品总数
      currentrow: ''
    }
  },

  computed: {
    headers() {
      return { token: getToken() }
    }
  },
  created() {
    var _this = this
    findAllBrand().then((res) => {
      if (res.success) {
        // console.log(res.data.items)
        _this.parduct = res.data.items
      }
    }),
    this.productsByPageAPI()
  },

  mounted() {},

  methods: {
    // 删除sku
    delrow(row) {
      var index = this.ruleForm.pmsSkuStockList.findIndex((el) => {
        return el.color === row.color && el.size === row.size
      })
      this.ruleForm.pmsSkuStockList.splice(index, 1)
      var skulist = [{ key: '颜色', value: row.color }, { key: '大小', 'value': row.size }]
      skulist = JSON.stringify(skulist)
      row.spData = skulist
      // skulist = JSON.parse(skulist)
      var _this = this
      updateProductAndSkus(this.ruleForm).then(res => {
        if (res.success) {
          _this.$message.success('删除成功')
        }
      })
    },
    // 保存sku'
    updata(row) {
      console.log(row)
      var skulist = [{ key: '颜色', value: row.color }, { key: '大小', 'value': row.size }]
      skulist = JSON.stringify(skulist)
      row.spData = skulist
      // skulist = JSON.parse(skulist)
      var _this = this
      updateProductAndSkus(this.ruleForm).then(res => {
        if (res.success) {
          _this.$message.success('更新成功')
        }
      })
      console.log(skulist)
    },
    // 新增sku列
    addskulist() {
      var obj = {
        pic: '', // 图片
        color: '', // 颜色
        size: '', // 大小
        stock: '', // 库存
        lowStock: '', // 预警库存
        lockStock: '', // 锁定库存
        skuCode: '16510', // sku编码
        price: '', // 价格
        sale: '' // 销量
      }
      this.ruleForm.pmsSkuStockList.push(obj)
    },
    // sku照片上传
    handleAvatarSuccessSKU(res, file) {
      this.ruleForm.pmsSkuStockList[this.currentrow].pic = URL.createObjectURL(
        file.raw
      )
      console.log(this.ruleForm.pmsSkuStockList[this.currentrow].pic)
    },
    // 获得sku行内id
    seletkrow(row) {
      var index = this.ruleForm.pmsSkuStockList.findIndex((el) => {
        return el.color === row.color && el.size === row.size
      })
      this.currentrow = index
      console.log(this.currentrow)
    },
    // 编辑sku
    addsku(row) {
      var _this = this
      productSkusDetail(row.id).then((res) => {
        console.log(res)
        if (res.success) {
          res.data.skus.forEach((el) => {
            var errs = JSON.parse(el.spData)
            el.color = errs[0].value
            el.size = errs[1].value
          })
          _this.ruleForm.product = res.data.product
          if (res.data.skus.length == 0) {
            var obj = {
              pic: '', // 图片
              color: '', // 颜色
              size: '', // 大小
              stock: '', // 库存
              lowStock: '', // 预警库存
              lockStock: '', // 锁定库存
              skuCode: '16510', // sku编码
              price: '', // 价格
              sale: '' // 销量
            }
            res.data.skus.push(obj)
            _this.ruleForm.pmsSkuStockList = res.data.skus
          } else {
            _this.ruleForm.pmsSkuStockList = res.data.skus
          }

          _this.dialogTableVisible = true
        }
      })
    },
    // 编辑
    handleClick(row) {
      this.$router.push({ path: '/product/addproduct', query: { id: row.id }})
    },
    // 新增
    addcaerted() {
      this.$router.push({ path: '/product/addproduct' })
    },
    // 重置
    reset() {
      this.$data.formInline = this.$options.data().formInline
    },
    // 查询
    seach() {
      this.$data.doot = this.$options.data().doot
      this.productsByPageAPI()
    },
    // 删除商品
    delPRT(row) {
      var _this = this
      delproduct(row.id).then((res) => {
        console.log(res)
        if (res.success) {
          _this.productsByPageAPI()
          _this.$message.success('删除成功')
        } else {
          _this.$message.error('删除失败')
        }
      })
    },
    // 最新
    newest(row) {
      var _this = this
      switchNewStatus({ productId: row.id, status: row.newStatus }).then(
        (res) => {
          console.log(res)
          if (res.success) {
            _this.productsByPageAPI()
            _this.$message.success('更新成功')
          } else {
            _this.$message.error('更新失败')
          }
        }
      )
    },
    // 推荐
    recommend(row) {
      var _this = this
      console.log(row.id, row.recommendStatus)
      switchRecommandStatus({
        productId: row.id,
        status: row.recommendStatus
      }).then((res) => {
        console.log(res)
        if (res.success) {
          _this.productsByPageAPI()
          _this.$message.success('更新成功')
        } else {
          _this.$message.error('更新失败')
        }
      })
    },
    // 发布
    issue(row) {
      var _this = this
      console.log(row.id, row.publishStatus)
      switchPublishStatus({
        productId: row.id,
        status: row.publishStatus
      }).then((res) => {
        console.log(res)
        if (res.success) {
          _this.productsByPageAPI()
          _this.$message.success('更新成功')
        } else {
          _this.$message.error('更新失败')
        }
      })
    },
    // 审核
    audit(row) {
      var _this = this
      console.log(row.id, row.verifyStatus)
      switchVerifyStatus({ productId: row.id, status: row.verifyStatus }).then(
        (res) => {
          console.log(res)
          if (res.success) {
            _this.productsByPageAPI()
            _this.$message.success('更新成功')
          } else {
            _this.$message.error('更新失败')
          }
        }
      )
    },
    // 分页器
    handleSizeChange(val) {
      this.doot.limit = val
      this.productsByPageAPI()
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.doot.start = val
      this.productsByPageAPI()
      console.log(`当前页: ${val}`)
    },
    handleChange(val) {
      this.productsByPageAPI()
      console.log(val)
    },
    // 更新商品列表
    productsByPageAPI() {
      var _this = this
      productsByPage(this.doot.start, this.doot.limit, this.formInline).then(
        (res) => {
          console.log(res)
          if (res.success) {
            _this.list = res.data.rows
            _this.total = res.data.total
          }
        }
      )
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
